package com.abe.mvp.view.adapter;

import java.io.File;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.abe.mvp.model.bean.CoverFlowBean;
import com.abe.strengthtrainer.R;
import com.squareup.picasso.Picasso;

@SuppressLint("InflateParams")
public class CoverFlowAdapter extends BaseAdapter {

	private ArrayList<CoverFlowBean> mData = new ArrayList<CoverFlowBean>(0);
	private Context mContext;

	public CoverFlowAdapter(Context context) {
		mContext = context;
	}

	public void setData(ArrayList<CoverFlowBean> data) {
		mData = data;
	}

	@Override
	public int getCount() {
		return mData.size();
	}

	@Override
	public Object getItem(int pos) {
		return mData.get(pos);
	}

	@Override
	public long getItemId(int pos) {
		return pos;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View rowView = convertView;

		if (rowView == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(R.layout.item_coverflow, null);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.text = (TextView) rowView.findViewById(R.id.label);
			viewHolder.image = (ImageView) rowView.findViewById(R.id.image);
			rowView.setTag(viewHolder);
		}

		ViewHolder holder = (ViewHolder) rowView.getTag();
		setImage(holder.image, position);
		holder.text.setText(mData.get(position).titleResId);
		return rowView;
	}

	private void setImage(ImageView imageView, int position) {
		if (mData.get(position).imageType.equals(Integer.class)) {
			Picasso.with(mContext).load((Integer) mData.get(position).imageResId)
					.into(imageView);
		} else if (mData.get(position).imageType.equals(String.class)) {
			/** 图片文件 */
			Picasso.with(mContext)
					.load(new File((String) mData.get(position).imageResId))
					.into(imageView);
		}
	}

	static class ViewHolder {
		public TextView text;
		public ImageView image;
	}
}
